package cn.edu.flying_birds;

import cn.edu.flying_birds.utils.Tools;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @PackageName: cn.edu.flying_birds
 * @ClassName: Column
 * @Description: 柱子类
 * @author: RZS
 * @date: 2021/6/8  0:26
 */
public class Column {
    // 柱子的图片
    BufferedImage img;
    // x坐标
    int x;
    // y坐标
    int y;
    // 柱子的宽度
    int width;
    // 柱子的高度
    int height;
    // 柱子之间的距离
    int distance = 245;
    // 创建随机数对象
    Random random = new Random();
    // 上下柱子的空隙
    int gap;

    public Column(int index) throws IOException {
        // 初始化柱子图片
        img = Tools.getImg("../../img/column.png");
        // 初始化x, y, width, height
        width = img.getWidth() / 2;
        height = img.getHeight() / 2;
        x = 320 + distance * (index == 1 ? 0 : 1);
        y = -(random.nextInt(height - (644 - 146) + 1));
        // 初始化空隙
        gap = 75;
    }

    /**
     * 柱子移动
     */
    public void move(){
        // 判断柱子是否完全跑出游戏界面，如果跑出，则回到下一根柱子后面
        if(x <= -width){
            x = 320 + distance;
            y = -(random.nextInt(height - (644 - 146) + 1));
        }
        x--;

    }
}
